package com.cskaoyan.service.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.user.AdminUserListBO;
import com.cskaoyan.bean.po.user.AdminFeedbackPO;
import com.cskaoyan.bean.po.user.AdminFootprintPO;
import com.cskaoyan.bean.po.user.AdminHistoryPO;
import com.cskaoyan.bean.vo.user.AdminFeedbackListVO;
import com.cskaoyan.bean.vo.user.AdminFeedbackVO;
import com.cskaoyan.bean.vo.user.AdminFootprintVO;
import com.cskaoyan.bean.vo.user.AdminHistoryVO;
import com.cskaoyan.mapper.user.AdminFeedbackMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/18 20:20
 */
@Service
@Data
public class AdminFeedbackServiceImpl implements AdminFeedbackService {
    private Integer limit;

    private Integer page;

    private Integer pages;

    private Integer total;

    private List<AdminFeedbackListVO> list;
    @Autowired
    AdminFeedbackMapper adminFeedbackMapper;

    @Override
    public AdminFeedbackVO printAdminFeedbackList(AdminUserListBO adminUserListBO) {

        page = adminUserListBO.getPage();
        limit = adminUserListBO.getLimit();

        Integer id = adminUserListBO.getId();
        String username = adminUserListBO.getUsername();

        PageHelper.startPage(page, limit);
        if (id == null && StringUtils.isEmpty(username)) {
            list = new ArrayList<>();
            // list = adminFeedbackMapper.selectList(null);
            List<AdminFeedbackPO> adminFeedbackPOS = adminFeedbackMapper.selectList(null);
            for (AdminFeedbackPO adminFeedbackPO : adminFeedbackPOS) {
                AdminFeedbackListVO adminFeedbackListVO = new AdminFeedbackListVO();
                try {
                    BeanUtils.copyProperties(adminFeedbackListVO, adminFeedbackPO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                adminFeedbackListVO.setPicUrls(StringUtils.splitJsonArrays(adminFeedbackPO.getPicUrls()));
                list.add(adminFeedbackListVO);
            }


        } else {
            list = new ArrayList<>();
            QueryWrapper<AdminFeedbackPO> adminUserListPOQueryWrapper = new QueryWrapper<>();

            adminUserListPOQueryWrapper.eq(!(id == null), "id", id);
            adminUserListPOQueryWrapper.like(!StringUtils.isEmpty(username), "username", username);
            List<AdminFeedbackPO> adminFeedbackPOS = adminFeedbackMapper.selectList(adminUserListPOQueryWrapper);
            for (AdminFeedbackPO adminFeedbackPO : adminFeedbackPOS) {
                AdminFeedbackListVO adminFeedbackListVO = new AdminFeedbackListVO();
                try {
                    BeanUtils.copyProperties(adminFeedbackListVO, adminFeedbackPO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                adminFeedbackListVO.setPicUrls(StringUtils.splitJsonArrays(adminFeedbackPO.getPicUrls()));
                list.add(adminFeedbackListVO);

            }
        }

            PageInfo<AdminFeedbackListVO> adminUserAddressPOPageInfo = new PageInfo<>(list);
            total = (int) adminUserAddressPOPageInfo.getTotal();
            pages = adminUserAddressPOPageInfo.getPages();
            AdminFeedbackVO adminFeedbackVO = new AdminFeedbackVO();
            BeanUtil.copyBean(this, adminFeedbackVO);
            return adminFeedbackVO;
        }
    }
